package com.hqj.zuul.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.hystrix.exception.HystrixTimeoutException;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 容错处理类，实现接口FallbackProvider
 */
@Component
public class TestFallBackProvider implements FallbackProvider {
    /**
     * 当前的容错处理逻辑是哪一个阶段。
     * 可以使用通配符'*',代表全部的服务都进行容错处理
     * @return
     */
    @Override
    public String getRoute() {
        /**
         * 返回一个提供服务的服务名称，应用名称
         * 好处-服务访问超时，那么指定的fallback逻辑执行
         */
        return "hqj1-eureka-client-8001";
    }

    @Override
    public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
        System.out.println("通过Hystrix实现容错处理");
        String msg = "";
        if(cause instanceof HystrixTimeoutException){
            msg = String.format("%s,访问时间超时!",route);
        }else{
            msg = route+",服务异常";
        }
        return fallbackResponse(msg,HttpStatus.OK);
    }


    public ClientHttpResponse fallbackResponse(String msg,HttpStatus status){
        //利用匿名内部类进行重写
        return new ClientHttpResponse() {
            /**
             * 返回的状态码
             * @return
             * @throws IOException
             */
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return status;
            }

            /**
             * 获取执行的状态码
             * @return
             * @throws IOException
             */
            @Override
            public int getRawStatusCode() throws IOException {
                return this.getStatusCode().value();
            }

            /**
             * 返回状态文本
             * @return
             * @throws IOException
             */
            @Override
            public String getStatusText() throws IOException {
                return this.getStatusCode().getReasonPhrase();
            }

            @Override
            public void close() {

            }

            /**
             * 返回值内容
             * @return
             * @throws IOException
             */
            @Override
            public InputStream getBody() throws IOException {
                String bodyText = String.format("{\"code\":999,\"message\":\"服务拒绝原因:%s\"}",msg);
                InputStream is = new ByteArrayInputStream(bodyText.getBytes());
                return is;
            }

            /**
             * 返回值的数据类型，头部封装json
             * @return
             */
            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                return headers;
            }
        };

    }

}
